<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/28
 * Time: 11:31
 */
namespace app\common\model;

use think\Cache;
use think\Model;

class Pdd extends Model{
	
	/**
	 * @var string 接口地址
	 */
	private $apiUrl = 'http://gw-api.pinduoduo.com/api/router';
	
	/**
	 * @var string POP分配给应用的client_id
	 */
	private $clientId = 'eb39e2c62e504dc8a2d985a79d6d72d6';
	
	/**
	 * @var string 密匙
	 */
	private $clientSecret = '3a6f1d9b4b75a7d3efeae87e635b932f06c202ea';
	
	/**
	 * pdd.ddk.goods.search（多多进宝商品查询）
	 * @param string $keyword 商品关键词，与opt_id字段选填一个或全部填写
	 * @param string $optId 商品标签类目ID，使用pdd.goods.opt.get获取
	 * @param int $page 默认值1，商品分页数
	 * @param int $pageSize 默认100，每页商品数量
	 * @param int $sortType 排序方式:0-综合排序;1-按佣金比率升序;2-按佣金比例降序;3-按价格升序;4-按价格降序;5-按销量升序;6-按销量降序;7-优惠券金额排序升序;8-优惠券金额排序降序;9-券后价升序排序;10-券后价降序排序;11-按照加入多多进宝时间升序;12-按照加入多多进宝时间降序;13-按佣金金额升序排序;14-按佣金金额降序排序;15-店铺描述评分升序;16-店铺描述评分降序;17-店铺物流评分升序;18-店铺物流评分降序;19-店铺服务评分升序;20-店铺服务评分降序;27-描述评分击败同类店铺百分比升序，28-描述评分击败同类店铺百分比降序，29-物流评分击败同类店铺百分比升序，30-物流评分击败同类店铺百分比降序，31-服务评分击败同类店铺百分比升序，32-服务评分击败同类店铺百分比降序
	 * @param string $withCoupon 是否只返回优惠券的商品，false返回所有商品，true只返回有优惠券的商品
	 * @param string $rangeList 范围列表，可选值：[{"range_id":0,"range_from":1,"range_to":1500},{"range_id":1,"range_from":1,"range_to":1500}]
	 * @param string $catId 商品类目ID，使用pdd.goods.cats.get接口获取
	 * @param string $goodsIdList 商品ID列表。例如：[123456,123]，当入参带有goods_id_list字段，将不会以opt_id、 cat_id、keyword维度筛选商品
	 * @param string $zsDuoId 招商多多客ID
	 * @param string $merchantType 店铺类型，1-个人，2-企业，3-旗舰店，4-专卖店，5-专营店，6-普通店（未传为全部）
	 * @return mixed
	 */
	
	public function goodsSearch($keyword = '', $page = 1, $pageSize = 100, $sortType = 8, $optId = '', $withCoupon = 'true', $rangeList = '', $catId = '', $goodsIdList = '', $zsDuoId = '', $merchantType = ''){
		$url = $this->apiUrl;
		
		$params = [
			'type' => 'pdd.ddk.goods.search',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'page' => $page,
			'page_size' => $pageSize,
			'sort_type' => $sortType,
			'keyword' => $keyword,
			'opt_id' => $optId,
			'cat_id' => $catId,
			'goods_id_list' => $goodsIdList,
			'zs_duo_id' => $zsDuoId,
			'with_coupon' => $withCoupon,
			'merchant_type' => $merchantType
		];
		$params['sign'] = $this->getSign($params);
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 商品详情
	 * @param $goodsId 列表返回的商品ID
	 * @return mixed
	 */
	public function goodsDetail($goodsId){
		$params = [
			'type' => 'pdd.ddk.goods.detail',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'goods_id_list' => '['.$goodsId.']'
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	/**
	 * 获取签名
	 * @param $params 请求参数
	 * @return string 加密sing
	 */
	public function getSign($params) {
		$sign = $this->clientSecret;
		if($params) {
			ksort($params);
			foreach($params as $k => $v) {
				$sign .= $k.$v;
			}
		}
		$sign .= $this->clientSecret;
		return strtoupper(md5($sign));
	}
	
	
	/**
	 * 生成推广位
	 * @param $number
	 * @return mixed
	 */
	public function generatePid($number){
		$params = [
			'type' => 'pdd.ddk.goods.pid.generate',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'number' => $number
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 获取推广位
	 * @return mixed
	 */
	public function queryPid(){
		$params = [
			'type' => 'pdd.ddk.goods.pid.query',
			'client_id' => $this->clientId,
			'timestamp' => time(),
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 多多进宝推广链接生成
	 * @param $pId 推广位ID
	 * @param $goodsId 商品ID，仅支持单个查询
	 * @param string $generateShortUrl 是否生成短链接，true-是，false-否
	 * @param string $multiGroup true--生成多人团推广链接 false--生成单人团推广链接（默认false）1、单人团推广链接：用户访问单人团推广链接，可直接购买商品无需拼团。2、多人团推广链接：用户访问双人团推广链接开团，若用户分享给他人参团，则开团者和参团者的佣金均结算给推手
	 * @param string $customParameters 自定义参数，为链接打上自定义标签。自定义参数最长限制64个字节。
	 * @param string $pullNew 是否开启订单拉新，true表示开启（订单拉新奖励特权仅支持白名单，请联系工作人员开通）
	 * @param string $generateWeappWebview 是否生成唤起微信客户端链接，true-是，false-否，默认false
	 * @param string $zsDuoId 招商多多客ID
	 * @return mixed
	 */
	public function generateUrl($pId, $goodsId, $customParameters = '', $generateShortUrl = 'true', $multiGroup = 'false',  $pullNew = 'true', $generateWeappWebview = 'true', $zsDuoId = ''){
		$params = [
			'type' => 'pdd.ddk.goods.promotion.url.generate',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'p_id' => $pId,
			'goods_id_list' => '['.$goodsId.']',
//			'generate_short_url' => $generateShortUrl,
//			'multi_group' => $multiGroup,
//			'custom_parameters' => $customParameters,
//			'pull_new' => $pullNew,
//			'generate_weapp_webview' => $generateWeappWebview,
			'generate_we_app' => 'true',
//			'zs_duo_id' => $zsDuoId,
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 生成红包推广链接
	 * @param $pId 推广位列表，例如：["60005_612"]
	 * @param string $generateShortUrl 是否生成短链接。true-是，false-否，默认false
	 * @param string $generate_we_app 是否生成小程序链接。true-是，false-否，默认false
	 * @param string $customParameters 自定义参数，为链接打上自定义标签。自定义参数最长限制64个字节。
	 * @param string $we_app_web_view_short_url 唤起微信app推广短链接
	 * @param string $generateWeappWebview 是否唤起微信客户端， 默认false 否，true 是
	 * @param string $we_app_web_wiew_url 唤起微信app推广链接
	 * @return mixed
	 */
	public function generatePromUrl($pId, $generateShortUrl = 'true', $generate_we_app = 'true', $customParameters = '', $we_app_web_view_short_url = 'true', $generateWeappWebview = 'true', $we_app_web_wiew_url = 'true'){
		$params = [
			'type' => 'pdd.ddk.rp.prom.url.generate',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'p_id_list' => '['.$pId.']',
			'generate_short_url' => $generateShortUrl,
			'generate_we_app' => $generate_we_app,
			'custom_parameters' => $customParameters,
			'generate_weapp_webview' => $generateWeappWebview,
			'we_app_web_view_short_url' => $we_app_web_view_short_url,
			'we_app_web_wiew_url' => $we_app_web_wiew_url,
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	
	/**
	 * 生成红包推广链接
	 * @param $pid 推广位ID
	 * @param $goodsId 商品ID
	 * @param string $customParameters 自定义参数，为链接打上自定义标签。自定义参数最长限制64个字节。
	 * @return mixed
	 */
	public function weappQrcodeUrlGen($pid, $goodsId, $customParameters = ''){
		$params = [
			'type' => 'pdd.ddk.weapp.qrcode.url.gen',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'p_id' => $pid,
			'goods_id_list' => '['.$goodsId.']',
			'custom_parameters' => $customParameters,
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 查询订单列表
	 * @param $startTime 开始时间
	 * @param $endTime 结束时间
	 * @param $page 页数
	 * @param int $page_size 每页数量
	 * @param string $pid 推广位
	 * @return mixed
	 */
	public function orderListIncrementGet($startTime, $endTime, $page, $page_size=100, $pid = ''){
		$params = [
			'type' => 'pdd.ddk.order.list.increment.get',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'start_update_time' => $startTime,
			'end_update_time' => $endTime,
			'page_size' => $page_size,
			'page' => $page,
		];
		$pid && $params['p_id'] = $pid;
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
	
	
	/**
	 * 查询订单列表
	 * @param int $pid 上级分类
	 * @return mixed
	 */
	public function goodsCatsGet($pid = 0){
		$params = [
			'type' => 'pdd.goods.cats.get',
			'client_id' => $this->clientId,
			'timestamp' => time(),
			'parent_cat_id' => $pid,
		];
		
		$params['sign'] = $this->getSign($params);
		$url = $this->apiUrl;
		$res = curlGetInfo($url, 'post', $params);
		return $res;
	}
}